'use strict';
import React from 'react';
import {
    AsyncStorage,
} from 'react-native';
import Storage from 'react-native-storage';

// 检测是否全为数字（str为''也返回true）
global.CheckIsNumberFunc = function(str) {
    var reg = /^[0-9]*$/;
    return reg.test(str);
}

// 检测字符串字符个数（包括汉字）
global.GetNickNameLengthFunc = function(str) {
    var reg = /[\u0391-\uFFE5]/g;
    return str.replace(reg, 'hz').length;
}

// 检测是否有空格
global.GetIsSpaceFunc = function(str) {
    var reg = /^\s*$/;
    return reg.test(str);
}

var storage = new Storage({
    // Use AsyncStorage for RN, or window.localStorage for web.
    // If not set, data would be lost after reload.
    storageBackend: AsyncStorage,
    // expire time, default 1 day(1000 * 3600 * 24 milliseconds).
    // can be null, which means never expire.
    defaultExpires: null,
    // cache data in the memory. default is true.
    enableCache: true,
    // if data was not found in storage or expired,
    // the corresponding sync method will be invoked and return 
    // the latest data.
    sync: {
        // we'll talk about the details later.
    }
})

export default class GCStorage {
	static read(key) {
		return new Promise(function (resolve, reject) {
			storage.load({
				key: key,
			}).then(ret => {
				resolve(ret);
			}).catch(err => {
				resolve('');
			});
		});
	}

	static save(key, data) {
        storage.save({
            key: key,
            rawData: data,
        });
	}

	static remove(key) {
		storage.remove({
			key: key
		});
	}
}

global.GCStorage = GCStorage;